import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;

public class IndexUpdateThread extends MyThread implements Runnable {

	private boolean stopFlag; // flag for stop thread(true - stop, false - run)
	private String threadName; // name of thread

	public IndexUpdateThread(Connection initConnection, DepositDBManager initDepositDBManager, DepositTypesDBManager initDepositTypesDBManager) {

		super(initConnection, initDepositDBManager, initDepositTypesDBManager);
		// TODO Auto-generated constructor stub
		setStopFlag(false);
		setThreadName("IndexUpdateThread");

	}

	public boolean isStopFlag() {
		return stopFlag;
	}

	public void setStopFlag(boolean stopFlag) {
		this.stopFlag = stopFlag;
	}

	public String getThreadName() {
		return threadName;
	}

	public void setThreadName(String threadName) {
		this.threadName = threadName;
	}

	// --------------------------------------------------------------------------------
	public void run() {

		while (!stopFlag) { // while thread running

			// Process run one time in month
			// Update interest_rate in deposits table
			// by interest_rate_change from deposit_types table
			// We call function recalcDeposit ( which updated estimated_amount
			// and final_estimated_amount for deposits table )

			ResultSet rset = null;
			Statement statement = null;
			boolean flagUpdateEstimatedAmount = false;
			DepositDBManager depositDBManager = super.getDepositDBManager();
			DepositTypesDBManager depositTypesDBManager = super.getDepositTypesDBManager();

			System.err.println("[ " + getThreadName() + " ]:" + " thread is running ");
			System.out.println("[ " + getThreadName() + " ]:" + " thread is running ");

			Deposit deposit = new Deposit(); // create new Deposit Object
			Connection conn = getConnection();// receive connection

			try {

				statement = conn.createStatement();
				String query = ("SELECT * FROM deposits");
				rset = statement.executeQuery(query);

				while (rset.next() == true) {
					// get Deposit Object from DB
					deposit.setDepositId(rset.getLong(1));
					deposit.setDepositeType(rset.getLong(2));
					deposit.setCustId(rset.getLong(3));
					deposit.setOpenningDate(rset.getDate(4));
					deposit.setEndingDate(rset.getDate(5));
					deposit.setLastCalcDate(rset.getDate(6));
					deposit.setAmount(rset.getDouble(7));
					deposit.setEstimatedAmount(rset.getDouble(8));
					deposit.setFinalEstimatedAmount(rset.getDouble(9));
					deposit.setInterestRate(rset.getDouble(10));

					// receive Deposit type object for current Deposit
					DepositType depositType = depositTypesDBManager.getDepositTypesObject(deposit.getDepositeType());

					// update interest_rate by interest_rate_changes for this
					// deposit_type
					Double updatedInterestRate = deposit.getInterestRate() + depositType.getInterestRateChange();

					System.err.println(" Deposit_id = " + deposit.getDepositId() + ",interest_rate updated = " + updatedInterestRate);

					System.out.println("[ " + getThreadName() + " ]: deposit_id = " + deposit.getDepositId() + ", interest_rate updated = " + updatedInterestRate);

					deposit.setInterestRate(updatedInterestRate);

					// Get today calendar Object
					Calendar today = Calendar.getInstance();
					// Call to calc Estimated Amount function
					flagUpdateEstimatedAmount = depositDBManager.calcEstimatedAmount(deposit, today);

					// check if estimated amount was updated
					if (flagUpdateEstimatedAmount == false) {
						System.err.println("[ " + getThreadName() + " ]: problem update estimated amount ");
					}

				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (DBManagerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// sleep to 1 month ( 1 sec = 1000 millisec ,
			// 24 hours = 8640000 , 1 month = 259200000 )
			try {
				Thread.sleep(5000); // sleep 1 month == 5 sec for out testing
			} catch (Exception e) {
				// TODO: handle exception
			}

		} // while
	}

	// --------------------------------------------------------------------------------
	public void stopRunning() {
		// stop thread running

		setStopFlag(true);
		System.out.println("[ " + getThreadName() + " ]:" + "thread stop running ");

	}
	// ------------------------------------------------------------------------------
}